package com.itqdkj.health.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itqdkj.health.constant.MessageConstant;
import com.itqdkj.health.constant.RedisMessageConstant;
import com.itqdkj.health.entity.Result;
import com.itqdkj.health.utils.SMSUtils;
import com.itqdkj.health.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping(value = "/validateCode")
public class ValidateCodeMobileController {
    @Autowired
    JedisPool jedisPool;
    //在线预约，发送验证码
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        try {
            //1.生成验证码
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            System.out.println(code);
            //2.向对应输入的手机号发送验证码
            SMSUtils.sendShortMessage(telephone,code.toString());
            //3.将生成的验证码存放到redis中，用于对页面输入的验证码进行对比校验
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,5*60,code.toString());
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
    //登陆发送验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        try {
            //1.生成验证码
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            System.out.println(code);
            //2.向对应输入的手机号发送验证码
            SMSUtils.sendShortMessage(telephone,code.toString());
            //3.将生成的验证码存放到redis中，用于对页面输入的验证码进行对比校验
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,5*60,code.toString());
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
